package zengjin.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zengjin.common.PageResponse;
import zengjin.common.R;
import zengjin.model.dto.bulletin.AddBulletinRequest;
import zengjin.model.dto.bulletin.PageBulletinRequest;
import zengjin.model.dto.bulletin.UpdateBulletinRequest;
import zengjin.model.en.Bulletin;
import zengjin.model.vo.bulletin.BulletinVO;
import zengjin.service.BulletinService;

/**
 * @author 19324
 */
@Tag(name = "公告相关接口")
@RestController
@RequestMapping("/bulletin")
@Slf4j
public class BulletinController {

    @Resource
    private BulletinService bulletinService;


    //1.添加公告
    @Operation(summary = "添加公告")
    @PostMapping("/addBulletin")
    public R<Boolean> addBulletin(@RequestBody AddBulletinRequest addBulletinRequest) {
        //1.参数校验
        if (addBulletinRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean result = bulletinService.addBulletin(addBulletinRequest);
        //3.返回结果
        if (!result) {
            return R.error(false, "添加失败");
        }
        return R.success(true, "添加成功");
    }

    //2.更新公告
    @Operation(summary = "更新公告")
    @PostMapping("/updateBulletin")
    public R<Boolean> updateBulletin(@RequestBody UpdateBulletinRequest updateBulletinRequest) {
        //1.参数校验
        if (updateBulletinRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean result = bulletinService.updateBulletin(updateBulletinRequest);
        //3.返回结果
        if (!result) {
            return R.error(false, "更新失败");
        }
        return R.success(true, "更新成功");

    }

    //3.删除公告
    @Operation(summary = "删除公告")
    @PostMapping("/deleteBulletin")
    public R<Boolean> deleteBulletin(Long bulletinId) {
        //1.参数校验
        if (bulletinId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean result = bulletinService.removeById(bulletinId);
        //3.返回结果
        if (!result) {
            return R.error(false, "删除失败");
        }
        return R.success(true, "删除成功");
    }

    //4.分页获取公告
    @Operation(summary = "分页获取公告")
    @PostMapping("/pageBulletin")
    public R<PageResponse<BulletinVO>> pageBulletin(@RequestBody PageBulletinRequest pageBulletinRequest) {
        //1.参数校验
        if (pageBulletinRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<BulletinVO> result = bulletinService.pageBulletin(pageBulletinRequest);
        //3.返回结果
        if (result == null) {
            return R.error(null, "查询失败");
        }
        return R.success(result, "查询成功");
    }

    //5.获取公告详情
    @Operation(summary = "获取公告详情")
    @PostMapping("/getBulletinDetail")
    public R<BulletinVO> getBulletinDetail(Long bulletinId) {
        //1.参数校验
        if (bulletinId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Bulletin result = bulletinService.getById(bulletinId);
        //3.返回结果
       if(result == null){
           return R.error(null,"查询失败");
       }
       BulletinVO bulletinVO = BeanUtil.copyProperties(result, BulletinVO.class);
       return R.success(bulletinVO,"查询成功");
    }





}
